package com.jluzh10.learning.web.controller;

import com.jluzh10.learning.domain.Comment;
import com.jluzh10.learning.domain.CommentParame;
import com.jluzh10.learning.domain.User;
import com.jluzh10.learning.service.CommentParameService;
import com.jluzh10.learning.service.CourseCommentService;
import com.jluzh10.learning.utils.ResponseData;
import com.jluzh10.learning.web.converter.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/comment")
public class CommentController {

    private final CourseCommentService courseCommentService;
    private final CommentParameService commentParameService;

    @Autowired
    public CommentController(CourseCommentService courseCommentService, CommentParameService commentParameService) {
        this.courseCommentService = courseCommentService;
        this.commentParameService = commentParameService;
    }

    /**
     * 发表评论
     * @param comment 评论（包括内容，评分，课程id）
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData commentScopeAndContent(@RequestBody Comment comment, @LoginUser User user) throws Exception {


        if (user==null){
            return new ResponseData().fail("请先登录");
        }
        comment.setUserId(user.getId());
        courseCommentService.addScopeAndContent(comment);

        return new ResponseData().success();
    }


    /**
     * 查询某个课程的评价信息
     * @param courseId 课程id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/info/{courseId}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData findScopeAvg(@PathVariable(value = "courseId") int courseId)throws Exception{
        CommentParame commentParame= commentParameService.findCommentParame(courseId);
        return new ResponseData().success().data(commentParame);
    }

    /**
     * 查询所有评价
     * @param courseId 课程id
     * @param limit 数据条数
     * @param offset 页码
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData findComment(@RequestParam Integer courseId, @RequestParam Integer limit,
                                    @RequestParam Integer offset) throws Exception{

        List<Comment> commentList;
        commentList= courseCommentService.findComment(courseId,limit,offset);

        return new ResponseData().success().data(commentList);
    }


}
